//
//  ViewController.m
//  quanminzhekou
//
//  Created by zxl on 15/1/4.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.viewControllerType = ViewControllerLoadingType;
    
    _engineModel = [[EngineModel alloc] init];
    [_engineModel addObserver:self forKeyPath:@"isNeedUpdate" options:0 context:nil];
    
    self.tableView.type = TableViewUpdateTypeNone;
    self.tableView.refreshView.delegate = self;
    self.tableView.reloadMoreView.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (ViewControllerNoNetWorkType == self.viewControllerType) {
        [self updateData];
    } else {
        [self.tableView reloadData];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
}

- (UIView*)noNetView {
    if (nil == _noNetView) {
        UIViewController* noNetViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NoNetWorkViewController"];
        [self addChildViewController:noNetViewController];
        _noNetView = noNetViewController.view;
        if (8.0 > [UIDevice currentDevice].systemVersion.floatValue) {
            _noNetView.frame = CGRectMake(0, 57, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds) - 57);
        }
        [self.view addSubview:_noNetView];
        
        UIControl* ctrl = [[UIControl alloc] initWithFrame:_noNetView.bounds];
        ctrl.backgroundColor = [UIColor clearColor];
        [ctrl addTarget:self action:@selector(onTapNoNetView:) forControlEvents:UIControlEventTouchUpInside];
        [_noNetView addSubview:ctrl];
    }
    
    return _noNetView;
}

- (UIView*)loadingView {
    if (nil == _loadingView) {
        UIViewController* loadingViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LoadingViewController"];
        [self addChildViewController:loadingViewController];
        _loadingView = loadingViewController.view;
        if (8.0 > [UIDevice currentDevice].systemVersion.floatValue) {
            _loadingView.frame = CGRectMake(0, 57, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds) - 57);
        }
        [self.view addSubview:_loadingView];
    }
    
    return _loadingView;
}

- (void)setViewControllerType:(ViewControllerType)viewControllerType {
    _viewControllerType = viewControllerType;
    switch (_viewControllerType) {
        case ViewControllerNormalType:
            self.tableView.hidden = NO;
            self.noNetView.hidden = YES;
            self.loadingView.hidden = YES;
            break;
            
        case ViewControllerLoadingType:
            self.tableView.hidden = YES;
            self.noNetView.hidden = YES;
            self.loadingView.hidden = NO;
            break;
            
        case ViewControllerNoNetWorkType:
            self.tableView.hidden = YES;
            self.noNetView.hidden = NO;
            self.loadingView.hidden = YES;
            break;
            
        default:
            break;
    }
}

#pragma mark - 
- (void)onTapNoNetView:(UIControl*)ctrl {
    [self updateData];
}

#pragma mark - virtual method
- (void)updateData {
}

- (void)finishRefreshData {
    self.tableView.type = TableViewUpdateTypeNone;
    self.tableView.isRefreshLoading = NO;
    [self.tableView.refreshView refreshScrollViewDataSourceDidFinishedLoading:self.tableView];
    self.tableView.reloadMoreView.statue = ReloadMoreDropupDroping;
}

- (void)finishReloadMoreDataWithIsEnd:(BOOL)isEnd {
    self.tableView.type = TableViewUpdateTypeNone;
    if (isEnd) {
        self.tableView.reloadMoreView.statue = ReloadMoreDropupEnd;
    } else {
        self.tableView.reloadMoreView.statue = ReloadMoreDropupDroping;
    }
    [self.tableView.reloadMoreView didFinishedReloadWithScrollView:self.tableView];
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"isNeedUpdate"]) {
        if (_engineModel.isNeedUpdate) {
            [self updateData];
            self.engineModel.isNeedUpdate = NO;
        }
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self.tableView.refreshView refreshScrollViewDidEndDragging:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.tableView.refreshView refreshScrollViewDidScroll:scrollView];
    [self.tableView.reloadMoreView scrollViewDidScroll:scrollView];
}

#pragma mark - RefreshDropdownViewDelegate
- (void)refreshDropdownViewDidTriggerRefresh:(RefreshDropdownView*)refreshDropdownView {
    self.tableView.type = TableViewUpdateTypeRefresh;
    self.tableView.isRefreshLoading = YES;
    self.engineModel.fyStr = @"1";
    self.engineModel.isNeedUpdate = YES;
}

- (BOOL)refreshDropdownViewDataSourceIsLoading:(RefreshDropdownView*)refreshDropdownView {
    return self.tableView.isRefreshLoading;
}

#pragma mark - ReloadMoreDropupViewDelegate
- (void)reloadMoreDropupViewWillReloadData:(ReloadMoreDropupView *)reloadMoreDropupView {
    self.tableView.type = TableViewUpdateTypeReloadMore;
    
    NSString* fy = @"2";
    if ([self.engineModel.fyStr isKindOfClass:[NSString class]] && 0 != self.engineModel.fyStr.length) {
        NSInteger index = self.engineModel.fyStr.integerValue;
        index ++;
        fy = [NSString stringWithFormat:@"%zd", index];
    }
    
    self.engineModel.fyStr = fy;
    self.engineModel.isNeedUpdate = YES;
}

@end
